package com.example.module_patrol.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.module_patrol.R

class DateAdapter(
    private val dates: List<Pair<String, String>>,
    private val currentDay: Int
) : RecyclerView.Adapter<DateAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvWeekDay: TextView = view.findViewById(R.id.tvWeekDay)
        val tvDay: TextView = view.findViewById(R.id.tvDay)
        val root: View = view
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_date, parent, false)
        return ViewHolder(view)
    }
    // 在Adapter中添加方法
    fun getTodayPosition(): Int {
        return dates.indexOfFirst { it.second.toInt() == currentDay }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val (weekDay, day) = dates[position]
        holder.tvWeekDay.text = "周$weekDay"
        holder.tvDay.text = day

        // 高亮今天
        if (day.toInt() == currentDay) {
            holder.root.setBackgroundResource(R.drawable.bg_date_selected)
            holder.tvDay.setTextColor(Color.WHITE)
            holder.tvWeekDay.setTextColor(Color.WHITE)
        } else {
            holder.root.background = null
            holder.tvDay.setTextColor(Color.parseColor("#333333"))
            holder.tvWeekDay.setTextColor(Color.parseColor("#666666"))
        }
    }

    override fun getItemCount() = dates.size
}